# -*- coding: utf-8 -*-
import scrapy
from scrapy import Request
from douban.items import DoubanItem


class TopSpider(scrapy.Spider):
    name = 'top'
    allowed_domains = ['douban.com']
    def start_requests(self):
        url = 'https://movie.douban.com/top250'
        yield Request(url)


    def parse(self, response):
        item = DoubanItem()
        movies = response.xpath('//ol[@class="grid_view"]/li')
        for movie in movies:
            item['ranking'] = movie.xpath('.//div[@class="pic"]/em/text()').extract()[0]
            item['name'] = movie.xpath('.//div[@class="hd"]/a/span[1]/text()').extract()[0]
            item['score'] = movie.xpath('.//div[@class="star"]/span[@class="rating_num"]/text()').extract()[0]
            item['score_num'] = movie.xpath('.//div[@class="star"]/span[last()]/text()').extract()[0]
            yield item

        next_url = response.xpath('//span[@class="next"]/a/@href').extract()
        if next_url:
            next_url = 'https://movie.douban.com/top250' + next_url[0]
            yield Request(next_url)